package com.example.cs4.mapper;

import com.example.cs4.model.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserServiceMapper {

    @Select("SELECT * FROM user WHERE name = #{name}")
    User getUserByName(String name);

    @Select("SELECT * FROM user WHERE id = #{id}")
    User getUserById(Integer id);

    @Update("UPDATE user SET password = #{password} WHERE name = #{name}")
    User updateUser(String name, String password);

    @Delete("DELETE FROM user WHERE id = #{id}")
    int deleteUserById(Integer id);

    @Select("SELECT * FROM user WHERE name = #{name} AND password = #{password}")
    User login(@Param("name") String name, @Param("password") String password);

    @Insert("INSERT INTO user (name, password) VALUES (#{name}, #{password})")
    void saveUser(User user);
    //offset 偏移量
    @Select("SELECT * FROM user ORDER BY id LIMIT #{pageSize} OFFSET #{offset}")
    List<User> getPage(@Param("offset") int offset, @Param("pageSize") int pageSize);

   @Select("SELECT * FROM user WHERE name LIKE CONCAT('%', #{name}, '%')")
    List<User> getAll(String name);
}
